<?php

namespace App\Admin\Controllers\Company;

use App\Admin\Repositories\Company;
use App\Models\Region;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class CompanyController extends AdminController
{

    public $title = '公司';



    /**
     * Make a grid builder.
     * 列表
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Company(), function (Grid $grid) {
            $grid->id->sortable();
            $grid->column('name', '名称');
            $grid->column('tel', '电话');
            $grid->column('principal_name', '负责人');
            $grid->column('status', '状态')->display(function ($status) {
                return \App\Models\Company::STATUS[$status];
            });
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name', '公司名称');
                $filter->like('tel', '公司电话');
                $filter->like('principal_name', '公司负责人');
                $filter->equal('status', '状态')->select(\App\Models\Company::STATUS);
            });
        });
    }

    /**
     * Make a show builder.
     * 详情
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new \App\Models\Company, function (Show $show) {
            $show->id;
            $show->field('name', '名称');
            $show->field('short_name', '简称');
            $show->field('tel', '公司电话');
            $show->field('province_name', '省');
            $show->field('city_name', '市');
            $show->field('address', '详细地址');
            $show->field('urgency_tel', '紧急电话');
            $show->field('email', '邮箱');
            $show->field('web_site', '网址');
            $show->field('principal_name', '负责人姓名');
            $show->field('principal_phone', '负责人联系电话');
            $show->field('status', '状态')->as(function ($status){
                return \App\Models\Company::STATUS[$status];
            });
            $show->created_at;
            $show->updated_at;
        });
    }


    /**
     * 表单内容
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Company(), function (Form $form) {
            $form->display('id');
            $form->text('name','公司名称')->rules(function ($form){
                $rules = 'required|max:100|unique:company,name';
                $id = $form->model()->id;
                $rules .= ",{$id},id";
                $rules .= ',deleted_at,NULL';

                return $rules;
            });
            $form->text('short_name', '公司简称')->rules(function ($form){
                $rules = 'required|max:50|unique:company,short_name';
                $id = $form->model()->id;
                $rules .= ",{$id},id";
                $rules .= ',deleted_at,NULL';

                return $rules;
            });
            $form->text('tel', '公司电话')->rules('required|max:20');
            $form->select('province_id', '省')
                ->options(Region::selectOptionByPid())
                ->rules('required|numeric|exists:region,id')
                ->load('city_id', '/tool/region/getCity');
            $form->select('city_id', '市')->options(function ($id){
                if($id) {
                    $city_info = Region::query()->find($id);
                    return Region::selectOptionByPid($city_info->pid);
                }
                return  [];
            })->rules('required|numeric|exists:region,id');
            $form->hidden('province_name')->saving(function () use($form) {
                return Region::query()->find($form->province_id)->name;
            });
            $form->hidden('city_name')->saving(function () use($form) {
                return Region::query()->find($form->city_id)->name;
            });
            $form->text('address', '详细地址')->rules('required|max:100');
            //todo 地图经纬度获取
            //$form->html(view('admin/company/map'));
//            $form->text('lng', '经度')->rules('required');
//            $form->text('lat', '纬度')->rules('required');

            $form->text('urgency_tel', '紧急电话')->rules('max:20');
            $form->text('email', '邮箱')->rules('max:100');
            $form->text('web_site', '网址')->rules('max:100');
            $form->text('principal_name', '负责人姓名')->rules('required|max:50');
            $form->text('principal_phone', '负责人联系电话')->rules('max:100');
            $form->switch('status', '状态')->default(1)->saving(function ($status) {
                return $status? 1: 0;
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * 删除
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        if($id == 1)
            return response()->json(['status'=> false, 'message'=> '默认公司，禁止删除！']);

        return parent::destroy($id);

    }


}
